Inside Macintosh: Memory

Previous | Chapter Top | Chapter Contents | Next

Changing the Sizes of Relocatable and Nonrelocatable Blocks

You can use the GetHandleSize function and the SetHandleSize procedure to find out and change the logical size of a relocatable block, and you can use the GetPtrSize function and the SetPtrSize procedure to find out and change the logical size of a nonrelocatable block.

GetHandleSize

You can use the GetHandleSize function to find out the logical size of the relocatable block corresponding to a handle.

FUNCTION GetHandleSize (h: Handle): Size;
h
A handle to a relocatable block.

DESCRIPTION

The GetHandleSize function returns the logical size, in bytes, of the relocatable block whose handle is h . In case of an error, GetHandleSize returns 0.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for GetHandleSize are

Registers on entry

A0

Handle to the relocatable block

Registers on exit

D0

If >=0 , number of bytes in relocatable block

 

If <0 , result code

The trap dispatcher sets the condition codes before returning from a trap by testing the low-order word of register D0 with a TST.W instruction. Because the block size returned in D0 by _GetHandleSize is a full 32-bit long word, the word-length test sets the condition codes incorrectly in this case. To branch on the contents of D0, use your own TST.L instruction on return from the trap to test the full 32 bits of the register.

SPECIAL CONSIDERATIONS

You shouldn't call GetHandleSize at interrupt time because the heap might be in an inconsistent state.

RESULT CODES

noErr

0

No error

nilHandleErr

-109

NIL master pointer

memWZErr

-111

Attempt to operate on a free block

SetHandleSize

You can use the SetHandleSize procedure to change the logical size of the relocatable block corresponding to a handle.

PROCEDURE SetHandleSize (h: Handle; newSize: Size);
h
A handle to a relocatable block.
newSize
The desired new logical size, in bytes, of the relocatable block.

DESCRIPTION

The SetHandleSize procedure attempts to change the logical size of the relocatable block whose handle is h . The new logical size is specified by newSize . SetHandleSize might need to move the relocatable block to obtain enough space for the resized block. Thus, for best results you should unlock a block before resizing it.

An attempt to increase the size of a locked block might fail, because of blocks above and below it that are either nonrelocatable or locked. You should be prepared for this possibility.

SPECIAL CONSIDERATIONS

Because SetHandleSize allocates memory, you should not call it at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for SetHandleSize are

Registers on entry

A0

Handle to the relocatable block

D0

Desired new size of relocatable block

Registers on exit

D0

Result code

RESULT CODES

noErr

0

No error

memFullErr

-108

Not enough memory

nilHandleErr

-109

NIL master pointer

memWZErr

-111

Attempt to operate on a free block

SEE ALSO

Instead of using the SetHandleSize procedure to set the size of a handle to 0, you can use the EmptyHandle procedure, described on EmptyHandle .

GetPtrSize

You can use the GetPtrSize function to find out the logical size of the nonrelocatable block corresponding to a pointer.

FUNCTION GetPtrSize (p: Ptr): Size;
p
A pointer to a nonrelocatable block.

DESCRIPTION

The GetPtrSize function returns the logical size, in bytes, of the nonrelocatable block pointed to by p . In case of an error, GetPtrSize returns 0.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for GetPtrSize are

Registers on entry

A0

Pointer to the nonrelocatable block

Registers on exit

D0

If >=0 , number of bytes in nonrelocatable block

 

If <0 , result code

The trap dispatcher sets the condition codes before returning from a trap by testing the low-order word of register D0 with a TST.W instruction. Because the block size returned in D0 by _GetPtrSize is a full 32-bit long word, the word-length test sets the condition codes incorrectly in this case. To branch on the contents of D0, use your own TST.L instruction on return from the trap to test the full 32 bits of the register.

RESULT CODES

noErr

0

No error

memWZErr

-111

Attempt to operate on a free block

SetPtrSize

You can use the SetPtrSize procedure to change the logical size of the nonrelocatable block corresponding to a pointer.

PROCEDURE SetPtrSize (p: Ptr; newSize: Size);
p
A pointer to a nonrelocatable block.
newSize
The desired new logical size, in bytes, of the nonrelocatable block.

DESCRIPTION

The SetPtrSize procedure attempts to change the logical size of the nonrelocatable block pointed to by p . The new logical size is specified by newSize .

An attempt to increase the size of a nonrelocatable block might fail because of a block above it that is either nonrelocatable or locked. You should be prepared for this possibility.

SPECIAL CONSIDERATIONS

Because SetPtrSize allocates memory, you should not call it at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for SetPtrSize are

Registers on entry

A0

Pointer to the nonrelocatable block

D0

Desired new size of nonrelocatable block

Registers on exit

D0

Result code

RESULT CODES

noErr

0

No error

memFullErr

-108

Not enough memory

memWZErr

-111

Attempt to operate on a free block


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next